package com.ronipl.createtabs;


import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import approvalactivity.ApproveAction;


import com.ronipl.createtabs.model.valueobjects.IUserActionListItem;
import com.ronipl.createtabs.model.valueobjects.StarredAction;
import com.ronipl.createtabs.utilities.dummy.DummyRequestList;
import commentactivity.CommentAction;


/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class FragmentA extends Fragment   {
	
	ListView list;
	Button HomeRefreshBtn;
	TextView homeHeaderText;
	
	
	public FragmentA() {
		// Required empty public constructor
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View rootView = inflater.inflate(R.layout.fragment_a, container, false);
		
		//Onclicks
		HomeRefreshBtn = (Button)rootView.findViewById(R.id.RefreshHome);
		HomeRefreshBtn.setOnClickListener(mGlobalClickListener);		
		
		
		Typeface cus_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/opensansregular.ttf");
		homeHeaderText = (TextView)rootView.findViewById(R.id.header_bg_wrapper_home);
		homeHeaderText.setTypeface(cus_font);
		HomeRefreshBtn.setTypeface(cus_font);
		
		list = (ListView)rootView.findViewById(R.id.list_home);
		list.setAdapter(new homeBaseAdapter(this));

		return rootView;

	}

	@Override
	public void onActivityCreated(Bundle savedInstaceState) {
		super.onActivityCreated(savedInstaceState);
	}

	final OnClickListener mGlobalClickListener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			
			switch(v.getId()){	
				case R.id.RefreshHome:
					Toast.makeText(getActivity(), "Refresh handler click", Toast.LENGTH_LONG).show();
					
					Intent i = new Intent("com.ronipl.creatabs.FormListItemActivity");
					startActivity(i);
					break;
			}			
		}		
	};
	
		
}



class homeBaseAdapter extends BaseAdapter{
	
	List<IUserActionListItem>list;
	
	Context context;
	
	public homeBaseAdapter(FragmentA fragmentA){
		
		context = (Context)fragmentA.getActivity();
		list = DummyRequestList.getDummyRequestList();	
		
	}

	@Override
	public int getCount() {
	
		return list.size();
	}

	@Override
	public Object getItem(int position) {
	
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}
	
	class HomeViewHolder {
		
		//CONTENT COMING FROM JAVA
		TextView header;
		TextView headerInfo;
		TextView headerInfoDataB;
		TextView contentTitle;
		TextView headerInfoContents;
		TextView footerTitle;
		TextView footerContent;
		
		public HomeViewHolder(View view){
			
			//FIND ID CONTAINER of the DATA
			header = (TextView)view.findViewById(R.id.header_data_a);
			headerInfo = (TextView)view.findViewById(R.id.header_info);
			headerInfoDataB = (TextView)view.findViewById(R.id.header_info_data_b);
			contentTitle = (TextView)view.findViewById(R.id.content_tItle);
			headerInfoContents = (TextView)view.findViewById(R.id.contents);
			footerTitle = (TextView)view.findViewById(R.id.footer_title);
			footerContent = (TextView)view.findViewById(R.id.footer_content);	
			
		}
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		//a-sign the View parameter to a row(inflated template)
		View row = convertView;
		//a-sign the class holder to a holder set to null
		HomeViewHolder holder = null;
		
		if(row == null){
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.home_view_liist, parent, false);
			holder = new HomeViewHolder(row);
			row.setTag(holder);
			
			final ImageView StarredBtn = (ImageView)row.findViewById(R.id.starred_item);
			StarredBtn.setClickable(true);
			
			final ImageView CommentBtn = (ImageView)row.findViewById(R.id.comment_icon);
			CommentBtn.setClickable(true);
			
			final ImageView ApproveBtn = (ImageView)row.findViewById(R.id.approved_item);
			ApproveBtn.setClickable(true);
		
			
			StarredBtn.setOnClickListener(new StarredAction(StarredBtn));
			CommentBtn.setOnClickListener(new CommentAction());
			ApproveBtn.setOnClickListener(new ApproveAction());
			
		}
		else
		{
			holder = (HomeViewHolder)row.getTag();
		}
		
		IUserActionListItem data = list.get(position);
		
		holder.header.setText(data.getHeader());
		holder.headerInfo.setText(data.getHeaderInfo());
		holder.headerInfoDataB.setText(data.getHeaderInfoDataB());
		holder.contentTitle.setText(data.getContentTitle());
		holder.headerInfoContents.setText(data.getContent());
		holder.footerTitle.setText(data.getFooterTitle());
		holder.footerContent.setText(data.getFooterContent());
		
		Typeface cus_font = Typeface.createFromAsset(context.getAssets(), "fonts/opensansregular.ttf");
		holder.header.setTypeface(cus_font);
		holder.headerInfo.setTypeface(cus_font);
		holder.headerInfoDataB.setTypeface(cus_font);
		holder.contentTitle.setTypeface(cus_font);
		holder.headerInfoContents.setTypeface(cus_font);
		holder.footerTitle.setTypeface(cus_font);
		holder.footerContent.setTypeface(cus_font);
		
		return row;
	}


	
}

